#include "clock.h"
#include "definitions.h"
#include <math.h>
#include <stdio.h>
#include <time.h>
#include "../inc/st7735.h"

// 设置默认时间（2025年7月19日 12:00:00）
struct tm currentTime = {.tm_year = 125, // 2025 - 1900 = 125
                         .tm_mon  = 6,   // 7 - 1 = 6
                         .tm_mday = 19,
                         .tm_hour = 12,
                         .tm_min  = 0,
                         .tm_sec  = 0};

void UpdateClockDisplay(struct tm *time);
void RTC_Init(void)
{
    RTC_RTCCTimeSet(&currentTime);
    RTC_RTCCClockSyncEnable();
}

// 绘制时钟表盘
void DrawClockFace(void)
{
    // 绘制外圈
    ST7735_DrawCircle(CLOCK_CENTER_X, CLOCK_CENTER_Y, CLOCK_RADIUS, COLOR_WHITE);

    // 绘制秒刻度（60个刻度）
    for (int i = 0; i < 60; i++)
    {
        float angle  = i * 6;                                                  // 每个刻度间隔6度
        int   length = (i % 5 == 0) ? (CLOCK_RADIUS - 8) : (CLOCK_RADIUS - 5); // 小时刻度长，分钟刻度短
        int   x1     = CLOCK_CENTER_X + (int)(cos(DEG2RAD(angle)) * (CLOCK_RADIUS - 2));
        int   y1     = CLOCK_CENTER_Y + (int)(sin(DEG2RAD(angle)) * (CLOCK_RADIUS - 2));
        int   x2     = CLOCK_CENTER_X + (int)(cos(DEG2RAD(angle)) * length);
        int   y2     = CLOCK_CENTER_Y + (int)(sin(DEG2RAD(angle)) * length);

        // 每小时的刻度用白色，分钟刻度用灰色
        uint16_t color = (i % 5 == 0) ? COLOR_WHITE : COLOR_GRAY;
        ST7735_DrawLine(x1, y1, x2, y2, color);
    }

    // 绘制中心圆点
    ST7735_DrawCircle(CLOCK_CENTER_X, CLOCK_CENTER_Y, 3, COLOR_RED);
}

// 绘制指针
void DrawClockHands(struct tm *time)
{
    // 当前秒的精确角度
    float secondAngle  = (time->tm_sec * 6.0f) - 90.0f;
    int   secondLength = 55;
    int   secondX      = CLOCK_CENTER_X + (int)(cos(DEG2RAD(secondAngle)) * secondLength);
    int   secondY      = CLOCK_CENTER_Y + (int)(sin(DEG2RAD(secondAngle)) * secondLength);
    ST7735_DrawLine(CLOCK_CENTER_X, CLOCK_CENTER_Y, secondX, secondY, COLOR_BLUE);

    // 分针（考虑秒的偏移）
    float minuteAngle  = (time->tm_min * 6.0f) - 90.0f; //+ (time->tm_sec / 60.0f * 6.0f) - 90.0f;
    int   minuteLength = 45;
    int   minuteX      = CLOCK_CENTER_X + (int)(cos(DEG2RAD(minuteAngle)) * minuteLength);
    int   minuteY      = CLOCK_CENTER_Y + (int)(sin(DEG2RAD(minuteAngle)) * minuteLength);
    ST7735_DrawLine(CLOCK_CENTER_X, CLOCK_CENTER_Y, minuteX, minuteY, COLOR_GREEN);

    // 时针（考虑分钟的偏移）
    float hourAngle  = ((time->tm_hour % 12) * 30.0f) + (time->tm_min / 60.0f * 30.0f) - 90.0f;
    int   hourLength = 30;
    int   hourX      = CLOCK_CENTER_X + (int)(cos(DEG2RAD(hourAngle)) * hourLength);
    int   hourY      = CLOCK_CENTER_Y + (int)(sin(DEG2RAD(hourAngle)) * hourLength);
    ST7735_DrawLine(CLOCK_CENTER_X, CLOCK_CENTER_Y, hourX, hourY, COLOR_RED);
}

// 更新时钟显示

void UpdateClockDisplay(struct tm *time)
{
    // 清屏
    ST7735_FillScreen(COLOR_BLACK);

    // 显示时分秒
    ST7735_Printf16x16(32, 44, COLOR_YELLOW, COLOR_BLACK, 0, "%02d:%02d:%02d", time->tm_hour, time->tm_min,
                       time->tm_sec);

    // 显示日期信息
    ST7735_Printf16x16(24, 68, COLOR_YELLOW, COLOR_BLACK, 0, "%04d/%02d/%02d", time->tm_year + 1900, time->tm_mon + 1,
                       time->tm_mday);

    // 绘制表盘
    DrawClockFace();

    // 绘制指针
    DrawClockHands(time);
}

// 主循环中调用

void Clock_Task(void)
{
    RTC_RTCCTimeGet(&currentTime);

    UpdateClockDisplay(&currentTime);
}